package xing.application.service.isv.suite;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import xing.application.api.DingApi;
import xing.application.service.BaseService;
import xing.domain.isv.suite.Corp;
import xing.domain.isv.suite.CorpApp;
import xing.domain.isv.suite.CorpAuthInfo;
import xing.domain.isv.suite.CorpChannelApp;
import xing.infrastructure.data.CallResult;
import xing.infrastructure.util.IdWorker;
import xing.repository.isv.suite.CorpAppRepository;
import xing.repository.isv.suite.CorpChannelAppRepository;
import xing.repository.isv.suite.CorpRepository;

import java.util.List;

/********************************
 * 文 件 名 :CropSuiteAuthService.java
 * 创 建 人：陈天龙
 * 日    期：2017/8/24
 * 描   述：企业授权业务处理类
 * 版 本 号：v1.0.0
 ********************************/
@Service
public class CorpSuiteAuthService extends BaseService {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());
    @Autowired
    private DingApi dingApi;
    @Autowired
    private CorpRepository corpRepository;
    @Autowired
    private CorpAppRepository corpAppRepository;
    @Autowired
    private CorpChannelAppRepository corpChannelAppRepository;

    /**
     * 描    述：更新企业信息
     * 创 建 人：陈天龙
     * 日    期：2017/8/24
     * @param suiteToken
     * @param suiteKey
     * @param corpId
     * @param permanentCode
     * @return CallResult
     */
    @Transactional(rollbackFor = Exception.class)
    public CallResult syncCorpInfo(String suiteToken, String suiteKey, String corpId, String permanentCode) throws Exception{
        logger.info("suite_key:{},corp_id:{},suite_access_token:{},permanent_code:{}"
                ,suiteKey,corpId,suiteToken,permanentCode);
        //1.同步企业信息
        CorpAuthInfo corpAuthInfo = dingApi.getAuthInfo(suiteKey,corpId,suiteToken);
        Corp corp = new Corp();
        corp.setId(IdWorker.nextId());
        corp.setCorpId(corpId);
        corp.setCorpLogoUrl(corpAuthInfo.getAuth_corp_info().getCorp_logo_url());
        corp.setCorpName(corpAuthInfo.getAuth_corp_info().getCorp_name());
        corp.setIndustry(corpAuthInfo.getAuth_corp_info().getIndustry());
        corp.setInviteCode(corpAuthInfo.getAuth_corp_info().getInvite_code());
        corp.setInviteUrl(corpAuthInfo.getAuth_corp_info().getInvite_url());
        corp.setAuthenticated(corpAuthInfo.getAuth_corp_info().isIs_authenticated());
        corp.setAuthLevel(corpAuthInfo.getAuth_corp_info().getAuth_level());
        saveOrUpdateCorp(corp);

        //2.同步企业下的微应用信息
        List<CorpAuthInfo.Agent> agentList = corpAuthInfo.getAuth_info().getAgent();
        for (CorpAuthInfo.Agent agent : agentList) {
            CorpApp corpApp = new CorpApp();
            corpApp.setId(IdWorker.nextId());
            corpApp.setCorpId(corpId);
            corpApp.setAgentId(agent.getAgentid());
            corpApp.setAgentName(agent.getAgent_name());
            corpApp.setLogoUrl(agent.getLogo_url());
            corpApp.setAppId(agent.getAppid());
            saveOrUpdateCorpApp(corpApp);
        }

        //3.同步企业下的服务窗信息
        List<CorpAuthInfo.ChannelAgent> channelAgentList = corpAuthInfo.getChannel_auth_info().getChannelAgent();
        for (CorpAuthInfo.ChannelAgent agent : channelAgentList) {
            CorpChannelApp corpChannelApp = new CorpChannelApp();
            corpChannelApp.setId(IdWorker.nextId());
            corpChannelApp.setCorpId(corpId);
            corpChannelApp.setAgentId(agent.getAgentid());
            corpChannelApp.setAgentName(agent.getAgent_name());
            corpChannelApp.setLogoUrl(agent.getLogo_url());
            corpChannelApp.setAppId(agent.getAppid());
            saveOrUpdateCorpChannelApp(corpChannelApp);
        }

        return CallResult.ok();
    }
    /**
     * 描    述：同步企业信息
     * 创 建 人：陈天龙
     * 日    期：2017/8/24
     * @param corp
     */
    private void saveOrUpdateCorp(Corp corp)throws Exception{
        corpRepository.saveOrUpdateCorp(corp.getId(),corp.getCorpId(),corp.getInviteCode()
                ,corp.getIndustry(),corp.getCorpName()
                ,corp.getInviteUrl(),corp.getCorpLogoUrl(),corp.isAuthenticated(),corp.getAuthLevel());
    }

    /**
     * 描    述：同步企业下的服务窗信息
     * 创 建 人：陈天龙
     * 日    期：2017/8/24
     * @param app
     */
    private void saveOrUpdateCorpApp(CorpApp app)throws Exception{
        corpAppRepository.saveOrUpdateCorpApp(app.getId(),app.getCorpId(),app.getAppId(),app.getAgentId(),app.getAgentName(),app.getLogoUrl());
    }

    /**
     * 描    述：同步企业下的微应用信息
     * 创 建 人：陈天龙
     * 日    期：2017/8/24
     * @param corpChannelApp
     */
    private void saveOrUpdateCorpChannelApp(CorpChannelApp corpChannelApp)throws Exception{
        corpChannelAppRepository.saveOrUpdateCorpChannelApp(corpChannelApp.getId(),corpChannelApp.getCorpId()
                ,corpChannelApp.getAppId(),corpChannelApp.getAgentId()
                ,corpChannelApp.getAgentName(),corpChannelApp.getLogoUrl());
    }
}
